<?php

namespace App\Repos;

use App\Models\Depart;
use App\Models\DepartLog;
use App\Models\User;
use App\Services\Wechat;
use Log;

class DepartRepo 
{
    public static function get($where=[])
    {
    	return Depart::where($where)->select('id','name')->get();
    }

    public static function create($data)
    {
    	$rs['status'] = false;
        try {
            $flag   = Depart::create($data);
            // 配件记录
            $log['depart_id'] = $flag->id;
            $log['num']       = $flag->store_num;
            $log['type']      = 1;
            $log['remark']    = '配件'.$flag->name.'初始数量'.$flag->store_num.$flag->unit;
            DepartLog::create($log);
        } catch (\Exception $e) {
            $rs['msg'] = $e->getMessage();
            return $rs;
        }
        $rs['status'] = true;
        return $rs;
    }

    public static function updateStore($data,$type=3)
    {
        $rs['status'] = false;
        try {
            $depart   = Depart::find($data['depart_id']);
            $data['depart_name'] = $depart->name;
            $warning_num = $depart->warning_num;
            if($data['depart_num']>$depart->store_num) {
                $rs['msg'] = '库存不足';
                $data['store_num'] = $depart->store_num;
                self::sendmsg($data);
                return $rs;
            }
            if($type==3) {
                $depart->store_num = $depart->store_num-$data['depart_num'];     
            }else {
                $depart->store_num = $depart->store_num+$data['depart_num'];
            }
            $depart->save();
            
            $type_name = $type==3?'减少':'增加';
            // 配件记录
            $log['depart_id'] = $data['depart_id'];
            $log['num']       = $data['depart_num'];
            $log['type']      = $type;
            $log['remark']    = '配件'.$depart->name.'数量'.$type_name.$data['depart_num'].$depart->unit;
            DepartLog::create($log);
        } catch (\Exception $e) {
            $rs['msg'] = $e->getMessage();
            Log::info('库存操作报错：'.$e->getMessage());
            return $rs;
        }

        //库存不足提醒
        if($depart->store_num<=$depart->warning_num) {
            $data['store_num'] = $depart->store_num;
            self::sendmsg($data);
        }
        


        $rs['status'] = true;
        return $rs;
    }

    public static function  sendmsg($data)
    {
        $users = User::where('role_code','1')->get();
        foreach ($users as $key => $user) {
            if(empty($user->openid)) continue;
                $msg['openid'] = $user->openid;  //'oulzp1TrmdClZJrGm62DpyC7nG-c';          
                $msg['first']  = $data['depart_name'].'-库存不足预警通知'; 
                $msg['store_num']   = $data['store_num'];
                $msg['depart_name'] = $data['depart_name'];  
                $msg['created_at']  = date('Y-m-d H:i:s');    

                $msg['remark'] = '需出库:'.$data['depart_num'].'，库存剩余：'.$data['store_num'];
                $msg['url']    = '';
               // dd($msg);
                Wechat::sendMsgTpl('store_warning',$msg);
        }           
                
          
    }
}
